package com.cheng.example.easypoi.charper01.j01;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.hutool.core.date.DateUtil;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalTime;
import java.util.*;

/**
 * @author chengshuai
 * @version 1.0
 * @date 2020/6/22 13:55
 */
public class ExcelSignUtil {


    /**
     * 导出
     */
    @SneakyThrows
    public static void exportExcel(List<SignPerson> signPersonList) {

        // 创建表头信息
        List<ExcelExportEntity> headList = new ArrayList<ExcelExportEntity>();
        ExcelExportEntity entity1 = new ExcelExportEntity("姓名", "name");
        entity1.setMergeVertical(true);
        headList.add(entity1);
        headList.add(new ExcelExportEntity(null, "sign"));

        // 动态生成表头
        Set<Integer> detailsSet = new HashSet<>();
        signPersonList.stream().forEach(e -> {
            for (int i = 0; i < e.getSingDayDetailList().size(); i++) {
                SingDayDetail details = e.getSingDayDetailList().get(i);

                if (details.getSignInNote() == null) {

                }
                boolean signInFlag = details.getSignInNote() != null && details.getSignInNote().equals("漏刷");
                boolean signOffFlag = details.getSignOffNote() != null && details.getSignOffNote().equals("漏刷");

                if (signInFlag || signOffFlag) {
                    detailsSet.add(i);
                }

            }
        });


        List<Integer> intDetailsList = new ArrayList(detailsSet);
        Collections.sort(intDetailsList);

        SignPerson signPerson1 = signPersonList.get(0);
        List<SingDayDetail> person1DetailsList = signPerson1.getSingDayDetailList();


        for (Integer integer : intDetailsList) {
            SingDayDetail details = person1DetailsList.get(integer);
            String format = DateUtil.format(details.getSignDate(), "MM/dd");
            ExcelExportEntity entity = new ExcelExportEntity(format, integer);
            headList.add(entity);
        }

        ExcelExportEntity outEntity = new ExcelExportEntity("出差", "out");
        outEntity.setMergeVertical(true);
        outEntity.setWidth(4);
        headList.add(outEntity);

        ExcelExportEntity leaveEntity = new ExcelExportEntity("请假", "leave");
        leaveEntity.setMergeVertical(true);
        leaveEntity.setWidth(4);
        headList.add(leaveEntity);

        ExcelExportEntity lateEntity = new ExcelExportEntity("漏刷/迟到/早退", "late");
        lateEntity.setMergeVertical(true);
        lateEntity.setWidth(15);
        headList.add(lateEntity);

        List<Map<String, Object>> bodyMapList = new ArrayList<>();

        int iFlag = 0;
        for (SignPerson signPerson : signPersonList) {

            String fill = null;

            if (iFlag % 2 == 0) {
                fill = "  ";
            } else {
                fill = " ";
            }

            List<SingDayDetail> detailsList = signPerson.getSingDayDetailList();

            Map map = new HashMap<>();
            map.put("name", signPerson.getName());
            map.put("sign", "签到");
            map.put("out", signPerson.getOutNum());
            map.put("leave", signPerson.getLeaveNum());
            map.put("late", signPerson.getLeaveNum());


            Map map1 = new HashMap<>();
            map1.put("name", signPerson.getName());
            map1.put("sign", "签退");
            map1.put("out", signPerson.getOutNum());
            map1.put("leave", signPerson.getLeaveNum());
            map1.put("late", signPerson.getLeaveNum());


            for (int i : intDetailsList) {

                SingDayDetail details = detailsList.get(i);

                if (details.getSignInNote() != null) {
                    map.put(i, details.getSignInNote());
                } else {
                    map.put(i, details.getSignInTime());
                }

                if (details.getSignOffNote() != null) {
                    map1.put(i, details.getSignOffNote());
                } else {
                    map1.put(i, details.getSignOffTime());
                }

            }

            bodyMapList.add(map);
            bodyMapList.add(map1);
        }

        ExportParams params = new ExportParams("2019年12月16日-27日考勤情况表", "我是sheet");

        //        params.setColor((short) 15);
        //        params.setStyle(ExcelExportStylerBorderImpl.class);
        //        params.setStyle(TestExportStyler.class);
        //        params.setHeaderColor((short) 41);

        Workbook workbook = ExcelExportUtil.exportExcel(params, headList, bodyMapList);
        File file = new File("D:/home/excel/v1");

        if (!file.exists()) {
            file.mkdirs();
        }

        FileOutputStream fos = new FileOutputStream("D:/home/excel/v1/ExcelExportForMap1.tt.xls");
        workbook.write(fos);
        fos.close();
    }


    /**
     * 导入
     *
     * @return
     */
    public static List<SignPerson> importExcel() {
        String year = null;
        String mouth = null;

        // 1、得到月份  todo 比较烂，以后待修正
        ImportParams params1 = new ImportParams();
        params1.setTitleRows(0);
        params1.setHeadRows(1);
        File file1 = new File("C:\\Users\\Medvalley\\Desktop\\work content\\015、考勤模块开发\\考勤统计.xlsx");
        List<Map<String, Object>> list1 = ExcelImportUtil.importExcel(file1, Map.class, params1);

        System.out.println("得到标题：" + list1.get(0).entrySet());
        for (Map.Entry entry : list1.get(0).entrySet()) {
            String key = (String) entry.getKey();
            //得到年
            year = key.substring(0, 4);
            //得到月
            mouth = key.substring(5, 7);
            System.out.println(year + "年" + mouth + "月");
            break;
        }
        // 2、得到个人打卡情况
        ImportParams params = new ImportParams();
        params.setHeadRows(2);
        params.setTitleRows(3);

        File file = new File("C:\\Users\\Medvalley\\Desktop\\work content\\015、考勤模块开发\\考勤统计.xlsx");
        List<Map<String, Object>> list = ExcelImportUtil.importExcel(file, Map.class, params);
        Map<String, Object> map = list.get(0);

        // 3、得到所有日期
        List<Integer> dayList = new ArrayList<>();
        map.entrySet().stream().forEach(e -> {
            if (e.getKey().contains("/")) {
                String key = e.getKey();
                String day = key.split("/")[0];
//                    System.out.println(day);
                dayList.add(Integer.valueOf(day));
            }
        });
        Collections.sort(dayList);
        List<SignPerson> signPersonList = new ArrayList<>();
        SignPerson signPerson;

        List<SingDayDetail> detailsListList = null;
        System.out.println(list.get(1));

        // 得到每个人的个人数据
        for (int i = 0; i < list.size(); i++) {

            map = list.get(i);
            if (i % 2 == 0) {
                signPerson = new SignPerson();
                detailsListList = new ArrayList<>();
                signPerson.setSingDayDetailList(detailsListList);
                signPerson.setName((String) map.get("姓名"));

                signPersonList.add(signPerson);
                for (Map.Entry e : map.entrySet()) {

                    for (Integer j : dayList) {
                        if (((String) e.getKey()).contains(String.valueOf(j))) {

                            // Excel导入
                            SingDayDetail singDayDetail = new SingDayDetail();
                            String value = (String) e.getValue();
                            singDayDetail.setSignDateNote((String) e.getKey());

                            // 日期
                            String dateStr = year + "-" + mouth + "-" + j;

                            // 设置日期
                            Date date = DateUtil.parse(dateStr);
                            singDayDetail.setSignDate(date);
                            if (value == null) {
                                detailsListList.add(singDayDetail);
                                continue;
                            }

                            if (value.contains("漏刷")) {
                                singDayDetail.setSignInNote("漏刷");
                                detailsListList.add(singDayDetail);
                                continue;
                            }

                            if (value.contains("迟到")) {
                                String[] values = value.split(" ");
                                String time = values[0];
                                String hours = time.split(":")[0];
                                String minute = time.split(":")[1];

                                // 创建时间
                                LocalTime localTime = LocalTime.of(Integer.valueOf(hours), Integer.valueOf(minute));
                                singDayDetail.setSignInNote("迟到");
                                singDayDetail.setSignInTime(localTime);
                                detailsListList.add(singDayDetail);
                                continue;
                            }


                            String hours = value.split(":")[0];
                            String minute = value.split(":")[1];

                            // 创建时间
                            LocalTime localTime = LocalTime.of(Integer.valueOf(hours), Integer.valueOf(minute));
                            singDayDetail.setSignInTime(localTime);
                            detailsListList.add(singDayDetail);
                        }
                    }
                }
            } else {

                for (Map.Entry e : map.entrySet()) {

                    for (SingDayDetail j : detailsListList) {
                        if (e.getKey().equals(j.getSignDateNote())) {
                            if (e.getValue() == null) {
                                continue;
                            }
                            String value = (String) e.getValue();

                            if (value.contains("漏刷")) {
                                j.setSignOffNote("漏刷");
                                continue;
                            }

                            if (value.contains("早退")) {
                                j.setSignOffNote("早退");
                                String[] values = value.split(" ");
                                String time = values[0];
                                String hours = time.split(":")[0];
                                String minute = time.split(":")[1];
                                // 创建时间
                                LocalTime localTime = LocalTime.of(Integer.valueOf(hours), Integer.valueOf(minute));
                                j.setSignOffTime(localTime);
                                continue;
                            }

                            String hours = value.split(":")[0];
                            String minute = value.split(":")[1];
                            LocalTime localTime = LocalTime.of(Integer.valueOf(hours), Integer.valueOf(minute));

                            j.setSignOffTime(localTime);
                        }
                    }

                }

            }
        }

        System.out.println(signPersonList.size());
        for (SingDayDetail s : signPersonList.get(0).getSingDayDetailList()) {
            System.out.println(s);
        }
        return signPersonList;
    }


    public static void main(String[] args) {
        List<SignPerson> signPersonList = importExcel();

        //
        SignPerson signPerson0 = signPersonList.get(0);
        SignPerson signPerson1 = signPersonList.get(1);
        SignPerson signPerson2 = signPersonList.get(2);


        signPerson0.setLeaveNum(1);
        signPerson1.setLeaveNum(1);
        signPerson2.setLeaveNum(1);

        exportExcel(signPersonList);
    }
}
